using AutoMapper;
using B.S.Data.MES.API.Applications.BaseData.Command.BOMCommand;
using B.S.Domain.BasicData.MaterialManagement.BOM;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using Microsoft.EntityFrameworkCore;
using NPOI.SS.Formula.Functions;
using Yitter.IdGenerator;

namespace B.S.Data.MES.API.Applications.BaseData.CommandHandlers.BOMCommandHandlers
{
    public class AddBOMCommandHandler : IRequestHandler<AddBOMCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<BOM> _bomRepository;
        private readonly IMapper _mapper;

        public AddBOMCommandHandler(IBaseRepository<BOM> bomRepository, IMapper mapper)
        {
            _bomRepository = bomRepository;
            _mapper = mapper;
        }

        public async Task<APIRequst<int>> Handle(AddBOMCommand request, CancellationToken cancellationToken)
        {
            APIRequst<int> result = new APIRequst<int>();
            var code = _bomRepository.GetAll().Where(x => x.Materialcode == request.Materialcode && !x.isDel).FirstOrDefault();
            if(code!=null)
            {
                result.Code = RequstCode.失败;
                result.Msg = RequstCode.失败.ToString() + "该BOM编码已存在，请重新输入!";
                return await Task.FromResult(result);
            }
            var bom = _mapper.Map<BOM>(request);
            bom.Materialcode=YitIdHelper.NextId().ToString();
            bom.CreateName = request.CreateName;
            bom.CreateTime = DateTime.Now;
            if(string.IsNullOrEmpty(bom.Materialcode))
            {
                result.Code = RequstCode.失败;
                result.Msg = RequstCode.失败.ToString() + "BOM编码不能为空!";
                return await Task.FromResult(result);
            }
            if (string.IsNullOrEmpty(bom.Bname))
            {
                result.Code = RequstCode.失败;
                result.Msg = RequstCode.失败.ToString() + "名称不能为空!";
                return await Task.FromResult(result);
            }
            if (bom.version<=0)
            {
                result.Code = RequstCode.失败;
                result.Msg = RequstCode.失败.ToString() + "版本不能小于0!";
                return await Task.FromResult(result);
            }
            if (string.IsNullOrEmpty(bom.describe))
            {
                result.Code = RequstCode.失败;
                result.Msg = RequstCode.失败.ToString() + "描述不能为空!";
                return await Task.FromResult(result);
            }
            if(request.ParentId.HasValue)
            {
                var ment = _bomRepository.GetAll().Where(x => x.BomId == request.ParentId.Value && !x.isDel).FirstOrDefault();
                if(ment==null)
                {
                    result.Code = RequstCode.失败;
                    result.Msg = RequstCode.失败.ToString() + "父级BOM不存在！!";
                    return await Task.FromResult(result);
                }
                bom.LevelId = ment.LevelId + 1;
                bom.ParentId = request.ParentId.Value;
                ment.IsLeaf = false;
                await _bomRepository.Update(bom);
            }
          var sidecode= await _bomRepository.Add(bom);
            if(sidecode>0)
            {
                result.Code = RequstCode.成功;
                result.Msg = RequstCode.成功.ToString();
                
            }
            else
            {
                result.Code = RequstCode.失败;
                result.Msg = RequstCode.失败.ToString();
            }
            return await Task.FromResult(result);
           
        }
    }
}